#include<iostream>
#include"contact.pb.h"
#include<fstream>
using namespace std;

void PrintContact(contact::Contacts& contacts)
{
   for(int i = 0 ; i < contacts.contacts_size() ;i++)
   {
      const contact::PeopleInfo& people =  contacts.contacts(i);
      cout << "联系人姓名: "<< people.name() << endl;
      cout << "联系人年龄:" << people.age() << endl;
      for(int j = 0 ; j < people.phone_size();j++)
      {
         const contact::PeopleInfo_Phone& phone = people.phone(j);
         cout << "联系人电话" << j+1 << ": " << phone.number();
         cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;  
      }
      ///是否设置any类型 && 存放的message是否是Address
      if(people.has_data() && people.data().Is<contact::Address>())
      {
         contact::Address address;
         ///Any->message
         people.data().UnpackTo(&address);
         if(!address.home_address().empty())
         cout << "联系人家庭地址:" << address.home_address() << endl;
         if(!address.unit_address().empty())
         cout << "联系人单位地址:" << address.unit_address() << endl;
      }
      
      switch (people.other_contact_case()) 
      {
         case contact::PeopleInfo::OtherContactCase::kQq:
             cout << "联系人qq:" << people.qq() << endl;
             break;
         case contact::PeopleInfo::OtherContactCase::kWechat:
             cout << "联系人微信:" << people.wechat() << endl;
             break;
         default:
           break;        
      }
      if(people.remark_size())
      {
          cout << "备注信息:" << endl;
          for(auto it = people.remark().begin();it!=people.remark().end();it++)
          {
            cout << "  " << it->first << ":" << it->second << endl;
          }
      }
      cout << "-----------------------------------------------------" << endl;
   }
}


int main()
{
    //1.读取本地已经存在的通讯录文件
    contact::Contacts contacts;
    fstream in("contact.bin",ios::binary | ios::in);
    if(in.is_open() == false)
    {
        cout << "打开通讯录文件失败!" << endl;
        return -1;
    }
    else if(!contacts.ParseFromIstream(&in))
    {
         cout << "parse error!" << endl;
         in.close();
         return -1;
    }
    //2.打印通讯录列表
    PrintContact(contacts);
}